package com.yanfan.iot.tdengine.init;


import com.yanfan.common.utils.spring.SpringUtils;
import com.yanfan.iot.tdengine.config.TDengineConfig;
import com.yanfan.iot.tdengine.config.TDengineDatabaseConfig;
import com.yanfan.iot.tdengine.dao.ZuTaiTDengineMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 类名: ApplicationStarted
 * 开发人: lsy
 */
@Component
public class ApplicationZutaiStarted implements ApplicationRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationZutaiStarted.class);

    @Autowired(required = false)
    private ZuTaiTDengineMapper ZuTaiTDengineMapper;
    @Autowired
    private TDengineDatabaseConfig databaseConfig;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //先获取TDengine的配置，检测TDengine是否已经配置
        if (SpringUtils.containBean(TDengineConfig.class)) {
            initTDengine();
            LOGGER.info("使用TDengine存储设备数据，初始化成功");
        } else {
            LOGGER.info("使用MySql存储设备数据，初始化成功");
        }
    }

    /**
     * @return
     * @Method
     * @Description 开始初始化加载系统参数, 创建数据库和超级表
     * @Param null
     * @date 2022/5/22,0022 14:27
     * @author wxy
     */
    public void initTDengine() {
        try {
            ZuTaiTDengineMapper.createDeviceHistoryDataSTable(databaseConfig.getDbName());
            LOGGER.info("完成超级表的创建");
        } catch (Exception e) {
            LOGGER.error("错误", e.getMessage());
            e.printStackTrace();
        }

    }
}
